<?php

class MY_Controller extends CI_Controller
{
    protected $layout = 'layout';
    protected $layoutData = '';

    protected $global_setting;     //公共配置参数

    protected $header;             //公共头部
    protected $footer;             //公共底部
    protected $nav_menu;           //公共菜单
    protected $other;              //其他（客服代码，统计代码）

    protected $data;               //body所有数据的存储容
    protected $Response;           //异步返回数据结构
    protected $lg_user;            //登录者信息

    public function __construct()
    {
        parent::__construct();

        //基本model
        $this->load->model('M_common');

        //处理URL中的数据
        $this->data = $this->uri->ruri_to_assoc();

        //异步结果返回
        $this->Response = array('code' => 201, 'message' => '', 'result' => array());

        //菜单选中键
        $_menu_key = $this->router->directory ? str_replace('/', '', $this->router->directory) : $this->router->class;
        $this->footer['menu_key'] = $_menu_key;

        //购物车数量
        $cart_arr = json_decode(gCookie('user_cart'), true);
        $_menu_num = is_array($cart_arr) ? count($cart_arr) : 0;
        $this->footer['menu_num'] = $_menu_num;

        //登录信息
        $lg_user_sign = gCookie('lg_user_sign');
        $lg_user_sign = LKDecode($lg_user_sign, HASHKEY);
        $lg_user_sign = json_decode($lg_user_sign, true);
        if (is_array($lg_user_sign) && count($lg_user_sign) > 0 && isset($lg_user_sign['id'])) {
            $this->lg_user = $lg_user_sign;
        } else {
            redirect('/welcome/index/');
        }
    }

    /*
    * 模板加载方法
    * $file    文件路径
    * $viewData    参数
    * $layoutData  模板参数
    */
    protected function render($file = NULL, &$viewData = array(), $layoutData = array('body_class' => 'layout_body'))
    {
        $data = array();

        $viewData['header'] = $this->load->view('public/header', $this->header, TRUE);
        $viewData['other'] = $this->load->view('public/other', $this->other, TRUE);
        $viewData['nav_menu'] = $this->load->view('public/nav_menu', $this->nav_menu, TRUE);
        $viewData['footer'] = $this->load->view('public/footer', $this->footer, TRUE);

        if (!is_null($file)) {
            $data['content'] = $this->load->view($file, $viewData, TRUE);
            $data['layout'] = $layoutData;
            $this->load->view($this->layout, $data);
        } else {
            $this->load->view($this->layout, $viewData);
        }
        $viewData = array();
    }

}
